package com.zhihesj.anan.utils;

import com.zhihesj.anan.system.entity.UserAuths;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * @author anan.
 * @version 1.0
 * @create 2020-09-05 16-54
 */
public class SecurityUtil {

    /**
     * 从security中获取登陆用户的用户名
     * @return
     */
    public static String getUser(){
        SecurityContext ctx = SecurityContextHolder.getContext();
        Authentication auth = ctx.getAuthentication();
        UserAuths userAuths = (UserAuths) auth.getPrincipal();
        return userAuths.getIdentifier();
    }
}
